#include "../src/Uart5.h"
#include "../src/GenericTypeDefs.h"
#include "../xc.h"

uint8_t PanelRxHead = 0;
uint8_t PanelRxTail = 0;
uint8_t PanelTxHead = 0;
uint8_t PanelTxTail = 0;
uint8_t PanelTxData[PANEL_BUF_LEN];
uint8_t PanelRxData[PANEL_BUF_LEN];

uint8_t PanelRxHead_CK = 0;
uint8_t PanelRxTail_CK = 0;
uint8_t PanelTxHead_CK = 0;
uint8_t PanelTxTail_CK = 0;
uint8_t PanelTxData_CK[PANEL_BUF_LEN];
uint8_t PanelRxData_CK[PANEL_BUF_LEN];

uint8_t testRxcount=0;
void PanelComm_RxTx(void)
{ 
    if((U5STA & _U5STA_URXDA_MASK) != 0U)
    {
        while((U5STA & _U5STA_URXDA_MASK) != 0U)
        {
            PanelRxData[PanelRxTail] = U5RXREG;
            PanelRxTail = (PanelRxTail + 1) & (PANEL_BUF_LEN - 1);
        }   
    }

    if((U5STA & _U5STA_UTXBF_MASK) == 0U)
    {
        while((U5STA & _U5STA_UTXBF_MASK) == 0U)
        {
            if(PanelTxHead != PanelTxTail)
            {
                U5TXREG = PanelTxData[PanelTxHead];
                PanelTxHead = (PanelTxHead + 1) & (PANEL_BUF_LEN - 1); 
            }  
            else
            {
                break;
            }
        }
    }
    
    
    if(PanelRxTail >= PanelRxHead)
    {
        testRxcount = PanelRxTail - PanelRxHead;//SPI6Back.Status2_Z.dat;//PanelCom.StationCmd;
    }
    else
    {
        testRxcount = 256 + PanelRxTail - PanelRxHead;
    }        

}